package org.community.picture.service.upload;

import lombok.Data;
import org.community.picture.repository.dao.CustomFile;
import org.community.picture.repository.util.MinioUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Data
public class UploadServiceImpl implements UploadService{

    @Resource
    private MinioUtil util;


    /**
     头像命名
     */
    @Override
    public CustomFile portraitRename(CustomFile portrait) {

        String originalFilename = portrait.getMultipartFile().getOriginalFilename();
        //上传文件类型限制已经在前端设置好
        assert originalFilename != null;
        String finalName = "/portrait/"+portrait.getUserId().toString()+".png";
        portrait.setObjectName(finalName);
        return portrait;
    }

    /**
     照片命名
     */
    @Override
    public CustomFile[] photosRename(CustomFile[] photos) {

        return new CustomFile[0];
    }

    /**
     * 单个文件上传

     */
    @Override
    public boolean upload(CustomFile portrait) {
        portrait = this.portraitRename(portrait);
        int resultCount = util.upload(new CustomFile[]{portrait}).size();
        return resultCount != 0;
    }


    /**多文件上传


     */
    @Override
    public boolean upload(CustomFile[] photos) {
        return false;
    }
}
